package com.xiaoliu.filter;
import com.xiaoliu.util.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

        /*

        if(request.getSession().getAttribute(Constant.USER_SESSION).level == VIP1){
             response.sendRedirect("/vip1/index.jsp");
        }
        if(request.getSession().getAttribute(Constant.USER_SESSION).level == VIP2){
             response.sendRedirect("/vip2/index.jsp");
        }
        if(request.getSession().getAttribute(Constant.USER_SESSION).level == VIP3){
             response.sendRedirect("/vip3/index.jsp");
        }
         */


        // ServletRequest       HttpServletRequest
        HttpServletRequest  request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        Object user_session = request.getSession().getAttribute(Constant.USER_SESSION);

        if (user_session == null){
            response.sendRedirect("/error.jsp");
        }

        chain.doFilter(req,resp);

    }

    @Override
    public void destroy() {

    }
}
